package com.taobao.txc.client.a.a;

import com.taobao.txc.a.b.g;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.TxcContext;
import com.taobao.txc.common.c.B;
import com.taobao.txc.common.c.l;
import com.taobao.txc.common.c.m;
import com.taobao.txc.common.c.n;
import com.taobao.txc.common.j;
import java.net.ConnectException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class a
  implements com.taobao.txc.client.a.a
{
  private static final LoggerWrap c = LoggerInit.logger;
  private com.taobao.txc.a.a.b d;
  public static BlockingQueue<String> a = new LinkedBlockingQueue();
  public static BlockingQueue<Long> b = new LinkedBlockingQueue();
  private static Set<String> e = Collections.synchronizedSet(new HashSet());
  private static a f = null;
  
  public static a c()
  {
    if (f == null) {
      synchronized (a.class)
      {
        if (f == null)
        {
          f = new a();
          new Thread(new b()).start();
        }
      }
    }
    return f;
  }
  
  public void a(com.taobao.txc.a.a.b paramb)
  {
    this.d = paramb;
  }
  
  public void a()
  {
    a(5);
  }
  
  public void b()
  {
    b(0);
  }
  
  public void a(long paramLong, String paramString1, String paramString2)
  {
    if (TxcContext.g()) {
      throw new com.taobao.txc.common.b.c("This transaction has been RT model!", com.taobao.txc.common.b.b.aB);
    }
    int i = TxcContext.getBeginCount();
    if ((i > 0) && (!TxcContext.inTxcTransaction()))
    {
      c.warn("beginCount:" + i + ",commitCount:" + TxcContext.a() + " not in transaction.");
      i = 0;
    }
    i++;
    TxcContext.setBegin(i);
    if (i == 1) {
      b(paramLong, paramString1, paramString2);
    } else {
      c.info("nesting transaction, level:" + i + ",xid:" + TxcContext.getCurrentXid());
    }
  }
  
  public void a(int paramInt)
  {
    int i = TxcContext.a();
    int j = TxcContext.getBeginCount();
    i++;
    TxcContext.a(i);
    if (i >= j)
    {
      TxcContext.c();
      if (TxcContext.inTxcTransaction()) {
        c(paramInt);
      }
    }
  }
  
  public void b(int paramInt)
  {
    
    if (TxcContext.inTxcTransaction()) {
      d(paramInt);
    }
  }
  
  private void b(long paramLong, String paramString1, String paramString2)
  {
    com.taobao.txc.common.c.b localb = new com.taobao.txc.common.c.b();
    localb.a(paramLong);
    localb.a(paramString1);
    localb.b(paramString2);
    com.taobao.txc.resourcemanager.c.a.a().a(localb);
    try
    {
      a(localb);
    }
    catch (Throwable localThrowable)
    {
      TxcContext.c();
      com.taobao.txc.resourcemanager.c.a.a().a(localb, localThrowable);
    }
  }
  
  private void a(com.taobao.txc.common.c.b paramb)
  {
    long l1 = 0L;
    if (c.c()) {
      l1 = System.currentTimeMillis();
    }
    com.taobao.txc.common.c.c localc = null;
    try
    {
      localc = (com.taobao.txc.common.c.c)this.d.a(paramb, 30000L);
      if (localc.a() != B.a.a()) {
        throw new com.taobao.txc.common.b.c(localc.a(), localc.b());
      }
      String str1 = localc.e();
      int i = str1.lastIndexOf(":") + 1;
      j = Integer.parseInt(str1.substring(i)) - 1000;
      TxcContext.bind(localc.d(), str1.substring(0, i) + j);
      if ((g.c != null) && (e.size() > 0) && (e.contains(TxcContext.e()))) {
        synchronized (g.d())
        {
          boolean bool = e.remove(TxcContext.e());
          if (bool)
          {
            String str5 = TxcContext.e();
            localIterator = g.d().iterator();
            while (localIterator.hasNext())
            {
              localo = (com.taobao.txc.a.b.o)localIterator.next();
              if (str5.equals(localo.c()))
              {
                localo.a(localo.a());
                g.e();
                break;
              }
            }
          }
        }
      }
    }
    catch (Throwable localThrowable)
    {
      int j;
      Iterator localIterator;
      com.taobao.txc.a.b.o localo;
      long l2;
      String str2;
      String str3;
      String str4;
      if ((g.c != null) && ((localThrowable instanceof ConnectException)))
      {
        str3 = localThrowable.getMessage();
        if ((str3 != null) && ((j = str3.lastIndexOf("/")) > 0))
        {
          ??? = str3.substring(j + 1);
          str4 = g.a().d((String)???);
          if (str4 != null) {
            g.a().a(null, str4);
          }
          g.a().a(null, (String)???);
          synchronized (g.d())
          {
            localIterator = g.d().iterator();
            while (localIterator.hasNext())
            {
              localo = (com.taobao.txc.a.b.o)localIterator.next();
              if (((String)???).equals(localo.c()))
              {
                if (localo.b() <= 3) {
                  break;
                }
                int k = localo.b() / 10;
                if (k < 3) {
                  k = 3;
                }
                localo.a(k);
                g.e();
                e.add(???);
                break;
              }
            }
          }
        }
      }
      else if (((localThrowable instanceof RuntimeException)) && (localThrowable.getMessage().indexOf("java.util.concurrent.TimeoutException") != -1))
      {
        str3 = localThrowable.getMessage();
        if ((str3 != null) && ((j = str3.lastIndexOf("ip:")) > 0))
        {
          ??? = str3.substring(j + 1);
          str4 = g.a().d((String)???);
          if (str4 != null) {
            g.a().a(null, str4);
          }
          g.a().a(null, (String)???);
        }
      }
      throw com.taobao.txc.common.b.c.a(localThrowable);
    }
    finally
    {
      if (c.c())
      {
        long l3 = System.currentTimeMillis();
        c.debug(localc + " cost " + (l3 - l1) + " ms.");
      }
      else if ((localc != null) && (localc.d() != null))
      {
        String str6 = localc.d();
        a.offer(str6);
        if (c.b())
        {
          com.taobao.txc.common.a.a.a().a(str6);
          c.a("tran.begin", new Object[] { str6 });
        }
      }
    }
  }
  
  private void c(int paramInt)
  {
    l locall = new l();
    if (TxcContext.getCurrentXid() == null) {
      throw new com.taobao.txc.common.b.c(B.b.a(), "the thread is not in transaction when invoke commit.");
    }
    locall.a(j.a(TxcContext.getCurrentXid()));
    long l1 = 0L;
    if (c.c()) {
      l1 = System.currentTimeMillis();
    }
    try
    {
      m localm = null;
      Object localObject1 = null;
      do
      {
        try
        {
          localm = (m)this.d.a(locall, 30000L);
          if (localm.a() == B.a.a())
          {
            if (c.b()) {
              c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "commit", Integer.valueOf(localm.a()), "" });
            }
            b.offer(Long.valueOf(locall.b()));
            long l3;
            return;
          }
          Thread.sleep(3000L);
        }
        catch (Exception localException)
        {
          if (localm != null) {
            c.a("GlobalCommit error", localm.b(), localException);
          } else {
            c.error("GlobalCommit error", localException.getMessage());
          }
          localObject1 = localException;
          try
          {
            Thread.sleep(3000L);
          }
          catch (InterruptedException localInterruptedException) {}
        }
        paramInt--;
      } while (paramInt > 0);
      if ((localm != null) && (localm.a() != B.a.a()))
      {
        if (c.b()) {
          c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "commit", Integer.valueOf(localm.a()), localm.b() });
        }
        throw new com.taobao.txc.common.b.c(localm.a(), "transaction " + TxcContext.getCurrentXid() + " Global commit failed. Msg:" + localm.b());
      }
      if (localObject1 != null)
      {
        if (c.b()) {
          c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "commit", Integer.valueOf(localm.a()), localObject1.getMessage() });
        }
        throw new com.taobao.txc.common.b.c(localObject1, "transaction " + TxcContext.getCurrentXid() + " Global commit failed.");
      }
      b.offer(Long.valueOf(locall.b()));
    }
    finally
    {
      long l2;
      TxcContext.unbind();
      if (c.c())
      {
        long l4 = System.currentTimeMillis();
        c.debug("invoke global commit message:" + locall + " cost " + (l4 - l1) + " ms.");
      }
    }
  }
  
  private void d(int paramInt)
  {
    n localn = new n();
    localn.a(j.a(TxcContext.getCurrentXid()));
    long l1 = 0L;
    if (c.c()) {
      l1 = System.currentTimeMillis();
    }
    com.taobao.txc.common.c.o localo = null;
    try
    {
      Object localObject1 = null;
      do
      {
        try
        {
          localo = (com.taobao.txc.common.c.o)this.d.a(localn, 30000L);
          if (localo.a() == B.a.a())
          {
            if (c.b()) {
              c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "rollback", Integer.valueOf(localo.a()), "" });
            }
            long l3;
            StringBuilder localStringBuilder2;
            return;
          }
          Thread.sleep(3000L);
        }
        catch (Exception localException)
        {
          if (localo != null) {
            c.a("GlobalRollback error", localo.b(), localException);
          } else {
            c.error("GlobalRollback error", localException.getMessage());
          }
          localObject1 = localException;
          try
          {
            Thread.sleep(3000L);
          }
          catch (InterruptedException localInterruptedException) {}
        }
        paramInt--;
      } while (paramInt > 0);
      if ((localo != null) && (localo.a() != B.a.a()))
      {
        if (c.b()) {
          c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "rollback", Integer.valueOf(localo.a()), localo.b() });
        }
        throw new com.taobao.txc.common.b.c(localo.a(), localo.b());
      }
      if (localObject1 != null)
      {
        if (c.b()) {
          c.a("tran.end", new Object[] { TxcContext.getCurrentXid(), "rollback", Integer.valueOf(localo.a()), localObject1.getMessage() });
        }
        throw new com.taobao.txc.common.b.c(localObject1);
      }
    }
    finally
    {
      long l2;
      StringBuilder localStringBuilder1;
      if (c.c())
      {
        long l4 = System.currentTimeMillis();
        c.debug("invoke global rollback message:" + localn + " cost " + (l4 - l1) + " ms.");
      }
      else
      {
        StringBuilder localStringBuilder3 = new StringBuilder("invoke global rollback message:");
        localStringBuilder3.append(localn);
        if (localo != null) {
          localStringBuilder3.append(",result:").append(localo.a()).append(",msg:").append(localo.b());
        }
        c.info(localStringBuilder3.toString());
      }
      TxcContext.unbind();
    }
  }
  
  public void d() {}
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.client.a.a.a
 * JD-Core Version:    0.7.0.1
 */